import constant from './constant'
import { JZ_SCORE_TYPE, TJ_SCORE_TYPE } from './baseData'
/*
  *  计算分数
  * */
function calculateScore(terms) {
  /*
  * goodTotal,checkTotal 违反扣分中 抽查合格数与 抽查总数
  * redeceScore 倒扣分数 （阈值扣分，重复扣分，直接扣分）
  * */
  const countParams = {
    goodTotal: 0,
    checkTotal: 0,
    reduceScore: 0
  }
  let checkItemCode = ''
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].checkItemCode) {
        checkItemCode = listEva[i].checkItemCode
      }
      if (listEva[i].result === -1) continue
      if (listEva[i].ruleType === 5) { // 违反扣分（目前只在实测实量、空鼓开裂中中使用） 计算在总分中
        countParams.goodTotal += listEva[i].goodTotal || 0
        countParams.checkTotal += listEva[i].checkTotal || 0
      } else {
        countParams.reduceScore += listEva[i].score || 0
      }
    }
  }
  const obj = {
    score: 0,
    scoreLevel: '',
    allNoCheck: false
  }
  if (checkItemCode === JZ_SCORE_TYPE.ZLATSG || checkItemCode === JZ_SCORE_TYPE.ZLCLGL || checkItemCode === TJ_SCORE_TYPE.ZLATSG || checkItemCode === TJ_SCORE_TYPE.ZLCLGL) { // 按图施工 与 材料管理 直接显示倒扣分
    obj.score = countParams.reduceScore.toFixed(2)
  } else if (countParams.checkTotal > 0) {
    obj.score = (countParams.goodTotal / countParams.checkTotal * 100 + countParams.reduceScore).toFixed(2)
  } else {
    obj.score = (100 + countParams.reduceScore).toFixed(2)
  }
  if (obj.score >= 90) {
    obj.scoreLevel = 'A'
  } else if (obj.score >= 80) {
    obj.scoreLevel = 'B'
  } else {
    obj.scoreLevel = 'C'
  }
  if (allNoCheck(terms)) {
    obj.score = ''
    obj.scoreLevel = ''
    obj.allNoCheck = true
  }
  return obj
}

/*
  *  不关联规则  合格点数 / 检查总点数 * 100
  *  计算 实测实量 分数
  * */
function calculateScslScore(terms, params) {
  /*
  * goodTotal,checkTotal 合格点数、检查总点数
  * */
  const countParams = {
    goodTotal: 0,
    checkTotal: 0
  }
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      countParams.goodTotal += listEva[i].goodTotal || 0
      countParams.checkTotal += listEva[i].checkTotal || 0
    }
  }
  const obj = {
    score: 0,
    scoreLevel: '',
    allNoCheck: false
  }
  if (countParams.checkTotal > 0) {
    obj.score = (countParams.goodTotal / countParams.checkTotal * 100).toFixed(2)
  }
  if (obj.score >= 90) {
    obj.scoreLevel = 'A'
  } else if (obj.score >= 80) {
    obj.scoreLevel = 'B'
  } else {
    obj.scoreLevel = 'C'
  }
  if (params && params.isConstruction) { // 土建 实测实量区分 高层与别墅
    obj.cqRdcZlScoreVO = {}
    if (params.productType === 'HIGH') { // 高层
      obj.cqRdcZlScoreVO.zlScslGcScore = obj.score
    } else {
      obj.cqRdcZlScoreVO.zlScslBsScore = obj.score
    }
  }
  if (allNoCheck(terms)) {
    obj.score = ''
    obj.scoreLevel = ''
    obj.allNoCheck = true
    if (params && params.isConstruction) {
      for (const key in obj.cqRdcZlScoreVO) {
        obj.cqRdcZlScoreVO[key] = ''
      }
    }
  }
  return obj
}

/*
  *  计算 空鼓开裂 分数
  * */
function calculateKgklScore(terms, params) {
  /*
  * goodTotal,checkTotal 违反扣分中 抽查合格数与 抽查总数 只在实测实量与空鼓开裂中 有
  * redeceScore 倒扣分数 （阈值扣分，重复扣分，直接扣分）
  * */
  const countParams = {
    reduceScore: 0,
    ytkf: 0, // 业态扣分 高层、别墅
    indoor: { // 户内
      goodTotal: 0,
      checkTotal: 0
    },
    publicArea: { // 公区
      goodTotal: 0,
      checkTotal: 0
    }
  }
  const noCheck = {
    indoor: true,
    publicArea: true
  }
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      if (listEva[i].ruleType === 5) { // 合格计分（目前只在实测实量、空鼓开裂中使用） 计算在总分中
        if (listEva[i].locationType === 'hn') { // 户内扣分
          countParams.indoor.goodTotal += listEva[i].goodTotal || 0
          countParams.indoor.checkTotal += listEva[i].checkTotal || 0
        } else if (listEva[i].locationType === 'gq') { // 公区扣分
          countParams.publicArea.goodTotal += listEva[i].goodTotal || 0
          countParams.publicArea.checkTotal += listEva[i].checkTotal || 0
        }
      } else { // 其他扣分计算在 总分倒扣分中
        if (listEva[i].deductObj === 'ytkf') {
          countParams.ytkf += listEva[i].score || 0
        } else {
          countParams.reduceScore += listEva[i].score || 0
        }
      }
      // 甩项 处理
      if (listEva[i].locationType === 'hn') { // 户内扣分
        noCheck.indoor = false
      } else if (listEva[i].locationType === 'gq') { // 公区扣分
        noCheck.publicArea = false
      }
    }
  }
  const obj = {
    score: 0,
    indoor: 0,
    publicArea: 0,
    scoreLevel: '',
    allNoCheck: false,
    noCheck: noCheck
  }

  if (countParams.indoor.checkTotal > 0) {
    obj.indoor = countParams.indoor.goodTotal / countParams.indoor.checkTotal * 100
  } else {
    obj.indoor = 100
  }
  if (countParams.publicArea.checkTotal > 0) {
    obj.publicArea = countParams.publicArea.goodTotal / countParams.publicArea.checkTotal * 100
  } else {
    obj.publicArea = 100
  }

  let allScore = 0
  let checkCount = 0 // 检查项数目
  if (!noCheck.indoor) {
    checkCount += 1
    allScore += obj.indoor
  } else { // 甩项得 0 分
    obj.indoor = 0
  }
  if (!noCheck.publicArea) {
    checkCount += 1
    allScore += obj.publicArea
  } else { // 甩项得 0 分
    obj.publicArea = 0
  }
  if (checkCount > 0) {
    if (params && params.isConstruction) { // 土建 空鼓开裂区分 高层与别墅
      obj.score = (allScore / checkCount + countParams.ytkf).toFixed(2)
      obj.reduceScore = countParams.reduceScore.toFixed(2)
    } else { // 精装 总分计分
      // obj.score = (allScore / checkCount + countParams.reduceScore).toFixed(2)
      obj.score = jzKgklScore(countParams)
    }
  } else {
    obj.score = countParams.reduceScore.toFixed(2)
  }

  if (params && params.isConstruction) {
    obj.cqRdcZlScoreVO = {}
    if (params.productType === 'HIGH') { // 高层
      obj.cqRdcZlScoreVO.zlKgGcScore = obj.score
      obj.cqRdcZlScoreVO.zlKgGcDecScore = obj.reduceScore
      obj.cqRdcZlScoreVO.zlKgGcGqScore = noCheck.publicArea ? '' : obj.publicArea
      obj.cqRdcZlScoreVO.zlKgGcHnScore = noCheck.indoor ? '' : obj.indoor
      obj.cqRdcZlScoreVO.zlKgGcBusdecScore = countParams.ytkf.toFixed(2)
    } else {
      obj.cqRdcZlScoreVO.zlKgBsScore = obj.score
      obj.cqRdcZlScoreVO.zlKgBsDecScore = obj.reduceScore
      obj.cqRdcZlScoreVO.zlKgBsGqScore = noCheck.publicArea ? '' : obj.publicArea
      obj.cqRdcZlScoreVO.zlKgBsHnScore = noCheck.indoor ? '' : obj.indoor
      obj.cqRdcZlScoreVO.zlKgBsBusdecScore = countParams.ytkf.toFixed(2)
    }
  } else if (params) {
    obj.cqRdcZlScoreVO = {} // 精装 空鼓开裂 户内与公区得分
    obj.cqRdcZlScoreVO.zlKgGqScore = noCheck.publicArea ? '' : obj.publicArea
    obj.cqRdcZlScoreVO.zlKgHnScore = noCheck.indoor ? '' : obj.indoor
  }

  // obj.score = ((obj.indoor + obj.publicArea) / 2 + countParams.reduceScore).toFixed(2)
  obj.indoor = obj.indoor.toFixed(2)
  obj.publicArea = obj.publicArea.toFixed(2)

  if (obj.score >= 90) {
    obj.scoreLevel = 'A'
  } else if (obj.score >= 80) {
    obj.scoreLevel = 'B'
  } else {
    obj.scoreLevel = 'C'
  }
  if (allNoCheck(terms)) { // 全部甩项
    obj.score = ''
    obj.indoor = ''
    obj.publicArea = ''
    obj.scoreLevel = ''
    obj.reduceScore = ''
    obj.allNoCheck = true
    if (params && params.isConstruction) {
      for (const key in obj.cqRdcZlScoreVO) {
        if (key.includes('DecScore')) {
          obj.cqRdcZlScoreVO[key] = 0
        } else {
          obj.cqRdcZlScoreVO[key] = ''
        }
      }
    }
  }
  return obj
  /*
  * 空鼓开裂 精装总分计分规则
  * （户内合格点数+公区合格点数）/总检查点数 * 100 - 扣分项
  * */
  function jzKgklScore(countParams) {
    const allCheck = countParams.indoor.checkTotal + countParams.publicArea.checkTotal
    const allGood = countParams.indoor.goodTotal + countParams.publicArea.goodTotal
    let score = 0
    if (allCheck > 0) {
      score = allGood / allCheck * 100 + countParams.reduceScore
    } else {
      score = 100 + countParams.reduceScore
    }
    score = score.toFixed(2)
    return score
  }
}

/*
  *  计算 观感 分数
  * */
function calculateGgScore(terms, params = {isConstruction: true}) {
  /*
  *  params: isConstruction 是否 土建
  * redeceScore 倒扣分数 （阈值扣分，重复扣分，直接扣分）
  * */
  const countParams = {
    reduceScore: 0,
    ytkf: 0, // 业态扣分
    indoor: { // 户内
      reduceScore: 0
    },
    publicArea: { // 公区
      reduceScore: 0
    },
    outsideWall: { // 外立面
      reduceScore: 0
    }
  }
  const noCheck = {
    indoor: true,
    publicArea: true,
    outsideWall: true
  }
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      if (listEva[i].deductObj === 'all') { // 整体扣分
        countParams.reduceScore += listEva[i].score || 0
      } else if (listEva[i].deductObj === 'ytkf') {
        countParams.ytkf += listEva[i].score || 0
      } else {
        if (listEva[i].locationType === 'hn') { // 户内扣分
          countParams.indoor.reduceScore += listEva[i].score || 0
        } else if (listEva[i].locationType === 'gq') { // 公区扣分
          countParams.publicArea.reduceScore += listEva[i].score || 0
        } else if (listEva[i].locationType === 'wlm' && params.isConstruction) { // 外立面 扣分(土建)
          countParams.outsideWall.reduceScore += listEva[i].score || 0
        }
      }
      // 甩项 处理
      if (listEva[i].locationType === 'hn') { // 户内扣分
        noCheck.indoor = false
      } else if (listEva[i].locationType === 'gq') { // 公区扣分
        noCheck.publicArea = false
      } else if (listEva[i].locationType === 'wlm' && params.isConstruction) { // 外立面 扣分(土建)
        noCheck.outsideWall = false
      }
    }
  }
  const obj = {
    score: 0,
    indoor: 0,
    publicArea: 0,
    scoreLevel: '',
    allNoCheck: false,
    noCheck: noCheck
  }

  obj.indoor = 100 + countParams.indoor.reduceScore
  obj.publicArea = 100 + countParams.publicArea.reduceScore
  if (params.isConstruction) { // 土建 观感 包含外立面 得分
    obj.outsideWall = 100 + countParams.outsideWall.reduceScore
    let allScore = 0
    let checkCount = 0 // 检查项数目
    if (!noCheck.indoor) {
      checkCount += 1
      allScore += obj.indoor
    }
    if (!noCheck.publicArea) {
      checkCount += 1
      allScore += obj.publicArea
    }
    if (!noCheck.outsideWall) {
      checkCount += 1
      allScore += obj.outsideWall
    }
    obj.outsideWall = obj.outsideWall.toFixed(2)
    if (checkCount > 0) {
      // obj.score = (allScore / checkCount + countParams.reduceScore).toFixed(2)
      obj.score = (allScore / checkCount + countParams.ytkf).toFixed(2) // 土建区分 高层与别墅 score = (高层 + 别墅) / 2 + 总倒扣分 后台计算
      obj.reduceScore = countParams.reduceScore.toFixed(2)
    } else {
      obj.score = countParams.reduceScore.toFixed(2)
    }
    obj.cqRdcZlScoreVO = {}
    if (params.productType === 'HIGH') { // 高层
      obj.cqRdcZlScoreVO.zlGgGcScore = obj.score
      obj.cqRdcZlScoreVO.zlGgGcDecScore = obj.reduceScore
      obj.cqRdcZlScoreVO.zlGgGcGqScore = noCheck.publicArea ? '' : obj.publicArea
      obj.cqRdcZlScoreVO.zlGgGcHnScore = noCheck.indoor ? '' : obj.indoor
      obj.cqRdcZlScoreVO.zlGgGcWlmScore = noCheck.outsideWall ? '' : obj.outsideWall
      obj.cqRdcZlScoreVO.zlGgGcBusdecScore = countParams.ytkf.toFixed(2)
    } else {
      obj.cqRdcZlScoreVO.zlGgBsScore = obj.score
      obj.cqRdcZlScoreVO.zlGgBsDecScore = obj.reduceScore
      obj.cqRdcZlScoreVO.zlGgBsGqScore = noCheck.publicArea ? '' : obj.publicArea
      obj.cqRdcZlScoreVO.zlGgBsHnScore = noCheck.indoor ? '' : obj.indoor
      obj.cqRdcZlScoreVO.zlGgBsWlmScore = noCheck.outsideWall ? '' : obj.outsideWall
      obj.cqRdcZlScoreVO.zlGgBsBusdecScore = countParams.ytkf.toFixed(2)
    }
  } else { // 精装 观感
    // 判断 甩项
    delete noCheck.outsideWall
    const ratio = {
      indoor: 0.7,
      publicArea: 0.3
    }
    let weight = 0 // 占比权重
    for (const key in noCheck) {
      if (!noCheck[key]) {
        weight += ratio[key]
      }
    }
    let allScore = 0
    if (weight > 0) {
      if (!noCheck.indoor) {
        allScore += obj.indoor * ratio.indoor / weight
      }
      if (!noCheck.publicArea) {
        allScore += obj.publicArea * ratio.publicArea / weight
      }
    }
    obj.cqRdcZlScoreVO = {} // 精装质量观感 户内与公区得分
    obj.cqRdcZlScoreVO.zlGgGqScore = noCheck.publicArea ? '' : obj.publicArea
    obj.cqRdcZlScoreVO.zlGgHnScore = noCheck.indoor ? '' : obj.indoor
    obj.score = (allScore + countParams.reduceScore).toFixed(2)
  }

  obj.indoor = obj.indoor.toFixed(2)
  obj.publicArea = obj.publicArea.toFixed(2)
  if (obj.score >= 90) {
    obj.scoreLevel = 'A'
  } else if (obj.score >= 80) {
    obj.scoreLevel = 'B'
  } else {
    obj.scoreLevel = 'C'
  }
  if (allNoCheck(terms)) {
    obj.score = ''
    obj.indoor = ''
    obj.publicArea = ''
    obj.scoreLevel = ''
    obj.allNoCheck = true
    obj.reduceScore = ''
    if (params && params.isConstruction) {
      for (const key in obj.cqRdcZlScoreVO) {
        if (key.includes('DecScore')) {
          obj.cqRdcZlScoreVO[key] = 0
        } else {
          obj.cqRdcZlScoreVO[key] = ''
        }
      }
    }
  }
  return obj
}

/*
*
* 判断是否全部 不可检查
* 返回值 boolean true 为全部不可检查 甩项
* */
function allNoCheck(terms) {
  let all = true
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result !== -1) {
        all = false
      }
    }
  }
  return all
}

/*
*  管理动作 子项得分计算
* */
function calculateGldzScore(terms, option, groupKeys) {
  /*
  * redeceScore 倒扣分数 （阈值扣分，重复扣分，直接扣分）
  * */
  const countParams = {
    mphnjfyzyb: { // 毛坯户内交付业主样板
      reduceScore: 0, // 扣分
      score: 0 // 得分
    },
    gqjfyzyb: { // 公区交付业主样板
      reduceScore: 0,
      score: 0
    },
    wlmjfyzyb: { // 外立面交付业主样板
      reduceScore: 0,
      score: 0
    },
    tjcpssx: { // 土建产品舒适性
      reduceScore: 0,
      score: 0
    },
    tjcnyzx: { // 土建-承诺一致性
      reduceScore: 0,
      score: 0
    },
    tjjsx: { // 土建-及时性
      reduceScore: 0,
      score: 0
    },
    tjtzyzx: { // 土建-图纸一致性
      reduceScore: 0,
      score: 0
    },
    tjwzx: { // 土建-完整性
      reduceScore: 0,
      score: 0
    },
    jzgczldf: { // 精装工程质量得分
      reduceScore: 0,
      score: 0
    },
    jzcpssx: { // 精装产品舒适性
      reduceScore: 0,
      score: 0
    },
    jzcnyzx: { // 精装-承诺一致性
      reduceScore: 0,
      score: 0
    },
    jzjsx: { // 精装-及时性
      reduceScore: 0,
      score: 0
    },
    jztzyzx: { // 精装-图纸一致性
      reduceScore: 0,
      score: 0
    },
    jzwzx: { // 精装-完整性
      reduceScore: 0,
      score: 0
    }
  }
  const sjdf = { // 设计得分
    tjScore: 0,
    jzScore: 0
  }
  const cbspkf = { // 成本适配扣分 分数负数
    tjScore: 0,
    jzScore: 0
  }
  const countKey = Object.keys(countParams)
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    if (countKey.some(key => key === terms[j].parentLevelCode)) { // 属于计分 项
      for (let i = 0; i < listEva.length; i++) {
        if (listEva[i].result === -1) continue
        countParams[terms[j].parentLevelCode].reduceScore += listEva[i].score || 0
      }
    } else if (terms[j].tableId === 'sjdf') {
      sjdf.tjScore = terms[j].score || 0
    } else if (terms[j].tableId === 'jzsjdf') {
      sjdf.jzScore = terms[j].score || 0
    } else if (terms[j].tableId === 'tjcbspkf') {
      cbspkf.tjScore = terms[j].score || 0
    } else if (terms[j].tableId === 'jzcbspkf') {
      cbspkf.jzScore = terms[j].score || 0
    }
  }

  const obj = {
    score: 0,
    result: 10 // 10 合格，11 不合格
  }
  if (option.gldzLevel === '2') { // 土建楼层会验
    let noCheckCount = 0 // 毛坯、户内、外立面 甩项数
    if (groupKeys.mphnjfyzyb && groupKeys.mphnjfyzyb.result === -1) { // 毛培甩项
      countParams.mphnjfyzyb.score = 0
      noCheckCount += 1
    } else {
      countParams.mphnjfyzyb.score = 100 + countParams.mphnjfyzyb.reduceScore
    }
    if (groupKeys.gqjfyzyb && groupKeys.gqjfyzyb.result === -1) {
      countParams.gqjfyzyb.score = 0
      noCheckCount += 1
    } else {
      countParams.gqjfyzyb.score = 100 + countParams.gqjfyzyb.reduceScore
    }
    if (groupKeys.wlmjfyzyb && groupKeys.wlmjfyzyb.result === -1) {
      countParams.wlmjfyzyb.score = 0
      noCheckCount += 1
    } else {
      countParams.wlmjfyzyb.score = 100 + countParams.wlmjfyzyb.reduceScore
    }
    const jlScore = noCheckCount === 3 ? 100 : (countParams.mphnjfyzyb.score + countParams.gqjfyzyb.score + countParams.wlmjfyzyb.score) / (3 - noCheckCount)
    obj.score = (0.5 * jlScore + sjdf.tjScore) + countParams.tjcpssx.reduceScore +
      countParams.tjcnyzx.reduceScore + countParams.tjjsx.reduceScore +
      countParams.tjtzyzx.reduceScore + countParams.tjwzx.reduceScore + cbspkf.tjScore
    if ((groupKeys.mphnjfyzyb.result !== -1 && countParams.mphnjfyzyb.score < 80) ||
      (groupKeys.gqjfyzyb.result !== -1 && countParams.gqjfyzyb.score < 80) ||
      (groupKeys.wlmjfyzyb.result !== -1 && countParams.wlmjfyzyb.score < 80) || obj.score < 80) {
      obj.result = 11
    } else {
      obj.result = 10
    }
    obj.mphnjfyzyb = groupKeys.mphnjfyzyb.result === -1 ? '' : countParams.mphnjfyzyb.score.toFixed(2)
    obj.gqjfyzyb = groupKeys.gqjfyzyb.result === -1 ? '' : countParams.gqjfyzyb.score.toFixed(2)
    obj.wlmjfyzyb = groupKeys.wlmjfyzyb.result === -1 ? '' : countParams.wlmjfyzyb.score.toFixed(2)
    obj.tjcpssx = countParams.tjcpssx.reduceScore.toFixed(2)
    obj.tjcnyzx = countParams.tjcnyzx.reduceScore.toFixed(2)
    obj.tjjsx = countParams.tjjsx.reduceScore.toFixed(2)
    obj.tjtzyzx = countParams.tjtzyzx.reduceScore.toFixed(2)
    obj.tjwzx = countParams.tjwzx.reduceScore.toFixed(2)
  } else if (option.gldzLevel === '3') { // 精装交付样板验收
    countParams.jzgczldf.score = 100 + countParams.jzgczldf.reduceScore
    obj.score = (0.5 * countParams.jzgczldf.score + sjdf.jzScore) + countParams.jzcpssx.reduceScore +
      countParams.jzcnyzx.reduceScore + countParams.jzjsx.reduceScore +
      countParams.jztzyzx.reduceScore + countParams.jzwzx.reduceScore + cbspkf.jzScore
    if (countParams.jzgczldf.score < 80 || obj.score < 80) {
      obj.result = 11
    } else {
      obj.result = 10
    }
    obj.jzgczldf = countParams.jzgczldf.score.toFixed(2)
    obj.jzcpssx = countParams.jzcpssx.reduceScore.toFixed(2)
    obj.jzcnyzx = countParams.jzcnyzx.reduceScore.toFixed(2)
    obj.jzjsx = countParams.jzjsx.reduceScore.toFixed(2)
    obj.jztzyzx = countParams.jztzyzx.reduceScore.toFixed(2)
    obj.jzwzx = countParams.jzwzx.reduceScore.toFixed(2)
  }
  obj.score = obj.score.toFixed(2)
  return obj
}

/*
*  管理动作 总项得分计算
* */
function calculateGldzAllScore(terms, inherit, ratio = {gxys: 0.5, ybys: 0.4, dagl: 0.1}) {
  /*
  * goodTotal,checkTotal 违反扣分中 抽查合格数与 抽查总数 只在实测实量与空鼓开裂中 有
  * redeceScore 倒扣分数 （阈值扣分，重复扣分，直接扣分）
  * */
  const countParams = {
    reduceScore: 0, // 扣除管理动作总分
    gxys: { // 工序验收（关键工序）
      reduceScore: 0,
      score: 0
    },
    ybys: { // 样板验收
      reduceScore: 0,
      score: 0
    },
    dagl: { // 档案管理
      reduceScore: 0,
      score: 0
    }
  }
  const noCheck = {
    gxys: true,
    ybys: true,
    dagl: true
  }
  let ybyspjAvg = 100 // 样板验收平均
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      let scoreType = null
      if (listEva[i].scoreType) {
        scoreType = listEva[i].scoreType.split('-')[1]
      } else {
        continue
      }
      if (listEva[i].courseCode === 'ybyspj') {
        ybyspjAvg = listEva[i].score // 样板验收平均分
      } else if (listEva[i].deductObj === 'all') { // 整体扣分
        countParams.reduceScore += listEva[i].score || 0
      } else {
        if (scoreType && countParams[scoreType]) {
          countParams[scoreType].reduceScore += listEva[i].score || 0
        }
      }
      // 甩项 处理
      if (scoreType === 'gxys') { // 工序验收
        noCheck.gxys = false
      } else if (scoreType === 'ybys') { // 样板验收
        noCheck.ybys = false
      } else if (scoreType === 'dagl') { // 档案管理
        noCheck.dagl = false
      }
    }
  }

  const obj = {
    score: 0,
    gxysScore: 0,
    ybysScore: 0,
    daglScore: 0,
    scoreLevel: '',
    allNoCheck: false,
    noCheck: noCheck
  }
  countParams.gxys.score = 100 + countParams.gxys.reduceScore
  countParams.ybys.score = ybyspjAvg + countParams.ybys.reduceScore
  countParams.dagl.score = 100 + countParams.dagl.reduceScore

  let weight = 0 // 占比权重
  for (const key in noCheck) {
    if (!noCheck[key]) {
      weight += ratio[key]
    }
  }
  let allScore = 0
  if (weight > 0) {
    if (!noCheck.gxys) {
      allScore += countParams.gxys.score * ratio.gxys / weight
    }
    if (!noCheck.ybys) {
      allScore += countParams.ybys.score * ratio.ybys / weight
    }
    if (!noCheck.dagl) {
      allScore += countParams.dagl.score * ratio.dagl / weight
    }
  }
  obj.score = (allScore + countParams.reduceScore).toFixed(2)
  obj.gxysScore = countParams.gxys.score.toFixed(2)
  obj.ybysScore = countParams.ybys.score.toFixed(2)
  obj.daglScore = countParams.dagl.score.toFixed(2)
  if (obj.gxysScore < 80 || obj.ybysScore < 80) {
    obj.scoreLevel = 'C'
  } else if (obj.score >= 90) {
    obj.scoreLevel = 'A'
  } else if (obj.score >= 80) {
    obj.scoreLevel = 'B'
  } else {
    obj.scoreLevel = 'C'
  }
  for (const key in noCheck) {
    if (noCheck[key]) {
      obj[key + 'Score'] = ''
    }
  }
  if (allNoCheck(terms)) {
    obj.score = ''
    obj.gxysScore = ''
    obj.ybysScore = ''
    obj.daglScore = ''
    obj.scoreLevel = ''
    obj.allNoCheck = true
  }
  return obj
}

/*
*  防渗漏等级 评估
* */
function antiseepEvaluation(terms, rule) {
  const count = { // 红黑条款数量
    blackCount: 0,
    redCount: 0,
    redstarCount: 0
  }
  const obj = {
    score: 'C',
    allNoCheck: false
  }
  const mergeItemIds = constant.fsl.mergeItemIds
  let mergeItemFirst = false // 合并项 是否 计算
  let directScope = '' // 直接评定等级
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      // 获取直接评定最低等级 directScope
      if (listEva[i].ruleType === 4) {
        let ruleJson = []
        try {
          ruleJson = JSON.parse(listEva[i].ruleJson)
        } catch (e) {
          console.log(e)
        }
        if (listEva[i].badTotal >= ruleJson[0].min) {
          if (directScope) {
            if (directScope.charCodeAt(0) < ruleJson[0].level.charCodeAt(0)) {
              // 更低直接评估等级 （asc码值更大）
              directScope = ruleJson[0].level
            }
          } else {
            directScope = ruleJson[0].level
          }
        }
      }
      // 计算红黑条款数量
      if (listEva[i].result === 11) {
        if (mergeItemIds.some(id => id === listEva[i].standardId)) { // 合并项另计
          if (mergeItemFirst) continue // 已计算合并项 跳过
          mergeItemFirst = true
        }
        if (listEva[i].itemLevel === '2') {
          count.redCount += 1
        } else if (listEva[i].itemLevel === '3') {
          count.redstarCount += 1
        } else if (listEva[i].itemLevel === '1') {
          count.blackCount += 1
        }
      }
      /* if (listEva[i].badTotal > 0) {
        if (listEva[i].itemLevel === '2') {
          count.redCount += 1
        } else if (listEva[i].itemLevel === '3') {
          count.redstarCount += 1
        } else {
          count.blackCount += 1
        }
      } else if (listEva[i].ruleType === 3) { // 符合条款
        if (listEva[i].result === 11) { // 不符合
          if (listEva[i].itemLevel === '2') {
            count.redCount += 1
          } else if (listEva[i].itemLevel === '3') {
            count.redstarCount += 1
          } else {
            count.blackCount += 1
          }
        }
      }*/
    }
  }
  if (allNoCheck(terms)) {
    obj.score = ''
    obj.allNoCheck = true
    return obj
  }
  // 此方法最后代码 包含return
  for (let i = 0; i < rule.length; i++) {
    for (let j = 0; j < rule[i].rules.length; j++) {
      if (count.redstarCount <= rule[i].rules[j].maxRedstar && count.redCount <= rule[i].rules[j].maxRed && count.blackCount <= rule[i].rules[j].maxBlack) {
        if (!directScope || directScope.charCodeAt(0) < rule[i].score.charCodeAt(0)) {
          // 更低评估等级 （asc码值更大）
          obj.score = rule[i].score
        } else {
          obj.score = directScope
        }
        return obj
      }
    }
  }
  return obj
}
/*
*  防渗漏 合并项处理 升级条款 等级处理
* */
function fslMergeItemHandle(terms) {
  const mergeItemIds = constant.fsl.mergeItemIds
  const mergeItems = [] // 抽出合并
  for (let j = 0; j < terms.length; j++) {
    const listEva = terms[j].standardList
    for (let i = 0; i < listEva.length; i++) {
      if (listEva[i].result === -1) continue
      if (mergeItemIds.some(id => id === listEva[i].standardId)) {
        mergeItems.push(listEva[i])
      }
    }
  }
  let allBadTotal = 0
  mergeItems.forEach(item => {
    allBadTotal += item.badTotal
  })
  const tempData = {...mergeItems[0]}
  tempData.badTotal = allBadTotal
  let result = 0 // 合并项结果
  let ruleJson = [] // 规则
  if (tempData.ruleJson) {
    try {
      ruleJson = JSON.parse(tempData.ruleJson)
    } catch (e) {
      console.log('initStandardScore', e)
    }
    if (ruleJson && ruleJson instanceof Array && ruleJson.length > 0) {
      if (ruleJson.some(rule => rule.min <= allBadTotal)) { // 判断结果是否合格
        result = 11
      } else {
        result = 10
      }
      upgradeTerms(tempData, ruleJson) // 设置结果
    }

    for (let j = 0; j < terms.length; j++) {
      const listEva = terms[j].standardList
      for (let i = 0; i < listEva.length; i++) {
        if (listEva[i].result === -1) continue
        if (mergeItemIds.some(id => id === listEva[i].standardId)) { // 设置合并项 渲染到页面
          if (result === 11) { // 不合格
            if (listEva[i].badTotal > 0) {
              listEva[i].result = 11
              listEva[i].itemLevel = tempData.itemLevel
            } else {
              listEva[i].result = 10
              listEva[i].itemLevel = listEva[i].standardLevel
            }
          } else {
            listEva[i].result = 10
            listEva[i].itemLevel = listEva[i].standardLevel
          }
        }
      }
    }
  }
}
/*
    *  升级条款 ruleType = 1
    * */
function upgradeTerms(data, json) {
  let min = ''
  let max = 0
  let maxLevel = ''
  let minLevel = ''
  // if (data.result !== -1) {
  //   if (data.badTotal > 0) {
  //     data.result = 11
  //   } else {
  //     data.result = 10
  //   }
  // }
  if (data.badTotal || data.badTotal === 0) {
    for (let i = 0; i < json.length; i++) {
      if (json[i].max > max || max === 0) {
        max = json[i].max
        maxLevel = json[i].level
      }
      if (json[i].min < min || min === '') {
        min = json[i].min
        minLevel = json[i].level
      }
      if (json[i].max) { // 存在最大值
        if (data.badTotal <= json[i].max && data.badTotal >= json[i].min) {
          data.itemLevel = json[i].level
          if (data.result !== -1) {
            data.result = 11
          }
        }
      } else {
        if (json[i].min > max) { // 最后一项没有最大值 max
          max = json[i].min
          maxLevel = json[i].level
        }
      }
    }
    if (data.badTotal >= max) {
      data.itemLevel = maxLevel
      if (data.result !== -1) {
        data.result = 11
      }
    }
    if (data.badTotal === min) {
      data.itemLevel = minLevel
      if (data.result !== -1) {
        data.result = 11
      }
    } else if (data.badTotal < min) {
      data.itemLevel = data.standardLevel
      if (data.result !== -1) {
        data.result = 10
      }
    }
  } else {
    data.itemLevel = data.standardLevel
    data.result = 0
  }
}

/*
   *  合格条款 违反标准  ruleType = 2
   * */
function violateStandard(data, json) {
  // console.log('violateStandard')
  if (data.badTotal >= json[0].min) {
    data.isViolateStandard = true
    if (data.result !== -1) {
      data.result = 11
    }
  } else {
    data.isViolateStandard = false
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
 *  直接评级  ruleType = 4
 * */
function directGrade(data, json) {
  // console.log('directGrade')
  if (data.badTotal >= json[0].min) {
    if (data.result !== -1 && json[0].level === 'C') {
      data.result = 11
    } else {
      data.result = 10
    }
  } else {
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
 *  合格计分（违反扣分）  ruleType = 5
 * */
function qualifiedScore(data, json) {
  console.log('qualifiedScore')
  if ((data.goodTotal || data.goodTotal === 0) && data.checkTotal) {
    if (data.result !== -1) {
      if (data.goodTotal / data.checkTotal >= 0.8) {
        data.result = 10
      } else {
        data.result = 11
      }
    }
  } else if (data.goodTotal === 0 && data.checkTotal === 0) {
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
   *  阈值扣分 ruleType = 6
   * */
function rangeReduceScore(data, json) {
  let min = 0
  let maxScore = 0
  let max = 0
  for (let i = 0; i < json.length; i++) {
    if (json[i].min && (min > json[i].min || min === 0)) {
      min = json[i].min
    }
    if (json[i].max && (max < json[i].max || max === 0)) {
      max = json[i].max
      maxScore = json[i].score
    }
    if (json[i].max) { // 存在最大值
      if (data.badTotal <= json[i].max && data.badTotal >= json[i].min) {
        data.score = -json[i].score
        if (data.result !== -1) {
          data.result = 11
        }
      }
    } else {
      if (json[i].min > max) {
        max = json[i].min
        maxScore = json[i].score
      }
    }
  }

  if (data.badTotal >= max) { // 大于最大值
    data.score = -maxScore
    if (data.result !== -1) {
      data.result = 11
    }
  }
  if (data.badTotal < min) { // 小于最小值
    data.score = 0
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
*  重复扣分 ruleType = 7
*  (data.badTotal - json[0].min) * json[0].score
* */
function repetiteReduceScore(data, json) {
  if (data.badTotal >= json[0].min) {
    data.score = -(data.badTotal + 1 - json[0].min) * json[0].score
    if (data.result !== -1) {
      data.result = 11
    }
  } else {
    data.score = 0
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
*  直接扣分 ruleType = 8
*  当 badTotal 超过 rule[0].min 直接扣分
* */
function directReduceScore(data, json) {
  if (data.badTotal >= json[0].min) {
    data.score = -json[0].score
    if (data.result !== -1) {
      data.result = 11
    }
  } else {
    data.score = 0
    if (data.result !== -1) {
      data.result = 10
    }
  }
}

/*
*  按类扣分 ruleType = 10
*  当 badTotal 表示扣分种类数量
* */
function categoryReduceScore(data, json) {
  repetiteReduceScore(data, json) // 重复
}
// 获取等级与分数 的结果
function getScoreLevel(val, scoreType) {
  if (!val) return ''
  if (scoreType === 'fsl') { // 防渗漏
    if (val === 'A') return 'VALID'
    if (val === 'B' || val === 'C') return 'UNVALID'
    return ''
  }
  if (val === 'A' || val >= 90) return 'EXCELLENT' // 优秀
  if (val === 'B' || (val < 90 && val >= 80)) return 'VALID' // 合格
  if (val === 'C' || val < 80) return 'UNVALID' // 不合格
  return ''
}
export default {
  allNoCheck,
  rangeReduceScore,
  repetiteReduceScore,
  directGrade,
  qualifiedScore,
  directReduceScore,
  categoryReduceScore,
  violateStandard,
  upgradeTerms,
  calculateScore,
  calculateScslScore,
  calculateKgklScore,
  calculateGgScore,
  calculateGldzScore,
  calculateGldzAllScore,
  antiseepEvaluation,
  fslMergeItemHandle,
  getScoreLevel
}
